package cn.liwq.demo.java.raw.factory;

/**
 * <p>Project: demo-common </p>
 * <p>Function:  </p>
 * <p>Description:  </p>
 * <p>Copyright: Copyright(c) 2018 Ucarinc </p>
 * <p>Company: Ucarinc </p>
 *
 * @author weiqiang.li@ucarinc.com
 * @version 1.0
 * @date 2018-08-16 17:10:11
 */
public class BeanFactory {

    private static volatile BeanFactory beanFactory = null;

    private BeanFactory() {
    }

    public static BeanFactory getInstance() {
        // 双重校验锁
        if (beanFactory == null) {
            synchronized (BeanFactory.class) {
                if (beanFactory == null) {
                    beanFactory = new BeanFactory();
                }
            }
        }
        return beanFactory;
    }

    public <T> T getBeanInstance(Class<T> clazz) {
        try {
            if (clazz.isInterface()) {
                System.out.println(clazz.getPackage().getName());;
            }
            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
